import { Component } from "cc";
import { BaseEntity } from "../entity/BaseEntity";
import { EntityVo } from "../vo/EntityVo";
import { ComponentType } from "../vo/GameConst";

export class BaseComponent extends Component{
    protected _vo:EntityVo;
    protected _obj:BaseEntity;
    protected _type:ComponentType;
    protected _isInit:boolean;
    protected _isRunning:boolean;
    public init():void{
        if(this._isInit) return;
        this._obj = this.getComponent(BaseEntity);
        this.onInit();
        this._isInit = true;
        this._isRunning = true;
    }

    protected update(dt: number): void {
        if(!this._isInit || !this._isRunning) return;
        this.onUpdate(dt);
    }

    public setData(vo:EntityVo){
        this._vo = vo;
        this._isRunning = true;
        this.updateVo();
    }

    public get vo():EntityVo{
        return this._vo;
    }

    public stop(isDestroy:boolean = false){
        this._isRunning = false;
        this._vo = null;
        this.onStop(isDestroy);
        if(isDestroy){
            this.destroy();
        }
    }

    protected updateVo(){}
    protected onInit(){}
    protected onUpdate(dt: number){}
    protected onStop(isDestroy:boolean = false){}
}